
<script>
function clickTimKiem()
	{
		if(document.getElementById ('formTimKiem').style.display == 'none')
		{
			document.getElementById ('formTimKiem').style.display = 'block';
		}
		else
		{
			document.getElementById ('formTimKiem').style.display = 'none';
		}
	}

</script>
<?php
 function word_split($str,$words) {
	$arr = preg_split("/[\s]+/", $str,$words+1);
	$arr = array_slice($arr,0,$words);
	return join(' ',$arr);
 }
//define a maxim size for the uploaded images in Kb
 define ("MAX_SIZE","300"); 

//This function reads the extension of the file. It is used to determine if the
// file  is an image by checking the extension.
	 function getExtension($str) {
			 $i = strrpos($str,".");
			 if (!$i) { return ""; }
			 $l = strlen($str) - $i;
			 $ext = substr($str,$i+1,$l);
			 return $ext;
	 }
		$errors=0;
		 if(isset($_POST['Submit'])) 
		 {
			 $tenanh = $_POST["tenanh"];
			$image=$_FILES['image']['name'];
			if ($image) 
			{
				$filename = stripslashes($_FILES['image']['name']);
				$extension = getExtension($filename);
				$extension = strtolower($extension);
				 if (($extension != "jpg") && ($extension != "png") && ($extension !="jpeg") && ($extension != "bmp") && ($extension != "gif") )
				 {
							echo "<script>alert('Không đúng định dạng file');</script>";
							$errors=1;
				  }
				  else
				  {
							 $size=filesize($_FILES['image']['tmp_name']);
							if ($size > MAX_SIZE*1024)
							{
								echo "<script>alert('Kích thước file không được quá 300KB');</script>";
								$errors=1;
							} else 
							{
								$image_name=time().'.'.$extension;
								$tenanh = $image_name;
								$newname="../img/cocautochuc/".$image_name;
								$copied = copy($_FILES['image']['tmp_name'], $newname);
								if (!$copied) 
								{
									echo '<h1>Up file lên server lỗi</h1>';
									$errors=1;
								}
							}
				  }
			}			
		
		 }
?>
	<div id="menu_tool">
    	<div id="tool_icon">
       		 <ul>
            	<li><a href="?go=addcocau"><img src="img/add.png" /></a></li>
            </ul>
            <ul>
                <li style="padding:0 5px 0 0px"><a href="?go=addcocau">Thêm Mới</a></li>
            </ul>
        </div>
    </div>	
    <?                  
			if (isset($_REQUEST['action'])) {
				$action = $_REQUEST['action'];
				switch($action) {
					case 'search':
					{
						$tieude = $_REQUEST['tieude'];	
						$trangthai = $_REQUEST['trangthai'];						
						$sql =" WHERE tentochuc LIKE '%$tieude%' ";							
						if ($trangthai<2) {
							$sql.= " AND status = $trangthai ";	
						}
						$_SESSION["search"] = $sql;
						$filtered = 'true';
					}
					break;
					
					case 'view':
					{
						$_SESSION["search"] = "";
					}
					break;
					
					case 'edit':
					{
								$id = $_REQUEST['id'];
								if ($errors == 1) {
									echo "<script>alert('Upload ảnh lỗi');</script>";
									echo "<script> window.history.go(-1);</script>";	
								} else {							
								$tentochuc = $_REQUEST['tentochuc'];
						//		$thoigian = $_REQUEST['thoigian'];
								$anh = $tenanh;
								$noidung = $_POST['noidung'];
								$status = $_REQUEST['trangthai'];
								$sql="select * from cocautochuc where idcocautochuc =$id";
								$result=mysql_query($sql) or die("Can not query cocautochuc in edit");
								if(mysql_num_rows($result)==0)
									echo "<script>alert('Lỗi khi sửa');</script>";
								else
								{
									$sql="update cocautochuc set tentochuc ='$tentochuc',status = $status,anhpreview = '$anh', noidung = '". mysql_real_escape_string($noidung)."' where idcocautochuc=$id";
									$result=mysql_query($sql) or die("Can not update cocautochuc ".mysql_error($conn));
									echo "<script>alert('Sửa thành công');</script>";		
								}
								echo "<script> location='?go=cocau&action=view&page=1';</script>";
								}
					}
					break;
					
					case 'add':
						{
							$tieude = $_REQUEST['tieude'];
							
							$noidung = $_REQUEST["noidung"];
					//		$thoigian = $_REQUEST['thoigian'];
							if ($errors == 1) {
									echo "<script>alert('Upload file lỗi');window.history.go(-1);</script>";
										
							} else {
							$anhpreview = $image_name;
							$status = $_REQUEST['trangthai'];
							$sql="insert cocautochuc(tentochuc, thoigian,noidung,anhpreview, status) values('$tieude', now() ,'". mysql_real_escape_string($noidung)."', '$anhpreview', $status)";
							$result=mysql_query($sql) or die("Can not insert cocautochuc " .mysql_error($conn));
							echo "<script> location='?go=cocau&action=view&page=1';</script>";
								}
						}
						break;
						
						case "del":
						{
								$id = $_REQUEST['id'];
								$sql="select * from cocautochuc where idcocautochuc= $id";
								$result=mysql_query($sql) or die("Can not query delete cocautochuc".mysql_error($conn));
								$row=mysql_fetch_array($result);
								if(mysql_num_rows($result)!= 0)
								{
									$sql="delete from cocautochuc where idcocautochuc = $id";
									$result=mysql_query($sql) or die("Can not delete cocautochuc".mysql_error($conn));
								}
								else
								{
									echo "<script> alert('Không thể xóa tổ chức này'); </script>";
								}
								echo "<script>alert('Xóa thành công'); location='?go=cocau&action=view&page=1';</script>";
						}
						break;
				}
			}
		?>
     
        <div id="bang">
    	<table border="1px"  bordercolor="#CCCCCC" cellpadding="0px" cellspacing="0px" cols="8" width="790px">
        		<tr>
				<td colspan="10" align="left"> &nbsp;<input type="button" value="Lọc Dữ Liệu" id="TimKiem" onclick="clickTimKiem()" style="padding:0 5px" /> 
				<form name="formTimKiem" id="formTimKiem" method="post" action="?go=cocau&action=search" style="display:none">
				<br />
					<table cellpadding="0" cellspacing="0" align="center" width="100%" border="0">
						<tr>
							<td width="40%" align="right"> Tiêu đề : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="tieude"  /> </td>
						</tr>
						<tr>
							<td align="right"> Trạng thái : &nbsp;</td>
							<td> &nbsp;<select name="trangthai">
								<option value = "2" selected="selected">Tất cả</option>
								<option value="1" >Hoạt động</option>
								<option value="0">Không hoạt động</option>
					
       			 				</select></td>
						</tr>
						<tr>
							<td colspan="2" align="center"> <input type="submit" value="Lọc dữ liệu" style="padding:0 5px" /></td>
						</tr>
					</table>
				<br />
				</form>
				</td>
              
			</tr>
           <!-- <tr>
            	<td colspan="4"></td>
            	<td colspan="2"><input type="button" id="themmoi" value="Thêm Mới" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="kichhoat" value="Kích Hoạt" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="vohieu" value="Vô Hiệu" style="padding: 0 5px"  /></td>                
            </tr>-->
			
            <th style="width:30px">STT</th>
            <th style="width:325px">Tiêu Đề</th>
            <th style="width:75px">Ảnh</th>
            <th style="width:40px">Trạng Thái<th>
            <th style="width:120px">Ngày Tạo</th>
            <th style="width:30px">Sửa</th>
 			<th style="width:30px">Xóa</th>
            <th style="width:70px">Kích Hoạt</th>
            <th style="width:70px">Vô Hiệu</th>
        <?
			$i = 0;
			$display  = 10;
			$pagenum = $_REQUEST["page"];
			$sql = "SELECT * FROM cocautochuc ";
			$sql .= $_SESSION["search"];
			$sql .= " ORDER BY thoigian DESC";
			$result=mysql_query($sql) or die("Can not query cocautochuc Order".mysql_error($conn));
				$totalpage=ceil(mysql_num_rows($result)/$display);
				if($pagenum<=0 ||$pagenum > $totalpage)
					$pagenum=1;
				$from=($pagenum-1)*$display;
				$sql.="  LIMIT $from, $display";
				$query=mysql_query($sql)  or die("Can not query cocautochuc Limit".mysql_error($conn));;					
				while ($row=mysql_fetch_array($query)) 
				{
					$temp = $row['idcocautochuc'];
					$i++;
			?>
				<tr>
					
					<td class="stt"><? echo $i ?></td>
					<td class="tieude"><? echo $row['tentochuc'] ?> </td>
					 <td class="anhpreview" id="thumbnail"><span><img src="../img/cocautochuc/<?=$row["anhpreview"]?>"/><br /><? echo word_split($row['anhpreview'],10); ?></span><? echo word_split($row['anhpreview'],4); ?> </td>
					<td class="trangthai" align="center"><? if ($row['status'] == 1) {
						echo '<img src="img/Check.jpg"/>';	
					} else {
						echo '<img src="img/Delete.jpg"/>';
					}
					?>
					</td>
					<td></td>
					<td class="thoigian"><?php echo ($time = date('d-m-Y G:i:s',strtotime($row['thoigian'])));?></td>
					<td class="chucnang" align="center"> <input type="button" value="Sửa" onClick="location='?go=editcocau&id=<?php echo $temp; ?>';" /></td>
					<td align="center"> <input type="button" value="Xoá" onClick="if (confirm('Bạn chắc chắn muốn xóa?') == true)
					{
						location='?go=cocau&action=del&id=<?php echo $temp; ?>';			
					}
					else
					return false;" style="width:100%;" /></td>
               		<td class="chucnang" align="center"> <input type="button" value="Kích Hoạt" onClick="if (confirm('Bạn muốn kích hoạt?') == true)
					{ location='?go=sttcocau&action=enable&id=<?php echo $temp; ?>'; }
					else return false;"  style="width:95%"/></td>
                    <td class="chucnang" align="center"> <input type="button" value="Vô Hiệu" onClick="if (confirm('Bạn muốn vô hiệu hóa?') == true)
					{ location='?go=sttcocau&action=disable&id=<?php echo $temp; ?>'; }
					else return false;"  style="width:95%"/></td>
				</tr>
			<?		
				} 
			
			?>
			<tr>
				<td colspan="3">Tổng số bản ghi: <? echo $i ?> </td>
                <td colspan="7">      	
                </td>
			</tr>
            
            </table>
    </div>
	    
	<div class="clear"></div>
</div>
          

